//
//  ALPUserViewController.m
//  AlpFM
//
//  Created by KING on 16/1/26.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPUserViewController.h"

@interface ALPUserViewController ()
<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIImageView *titleImageView;
@property (nonatomic, strong) UIImageView *bg;
@end

@implementation ALPUserViewController
-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [super viewWillAppear:animated];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"my";
    [self createTableView];
    
}
- (void)createTableView{
    self.myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.contentInset = UIEdgeInsetsMake(self.view.frame.size.height * 0.27, 0, 0, 0);
    [self.view addSubview:self.myTableView];
    
    //猫背景
    self.backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -self.view.frame.size.height * 0.30, self.view.frame.size.width, self.view.frame.size.height * 0.3)];
    self.backgroundImageView.image = [[UIImage imageNamed:@"find_radio_default@2x"]boxblurImageWithBlur:0.4];
//    self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    [self.myTableView addSubview:self.backgroundImageView];
    self.bg.userInteractionEnabled = YES;
    self.bg = [[UIImageView alloc]initWithFrame:CGRectMake(0, -self.view.frame.size.height * 0.3, self.view.frame.size.width, self.view.frame.size.height * 0.3)];
    self.bg.image = [UIImage imageNamed:@"find_pop_shadow@2x"] ;
    [self.myTableView addSubview:self.bg];
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"mineTableViewCell"];
    
    //头像
    self.titleImageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.bg.frame.size.width - self.bg.frame.size.width * 0.22) / 2, self.bg.frame.size.height - self.bg.frame.size.width * 0.45, self.bg.frame.size.width * 0.22, self.bg.frame.size.width * 0.22)];
    self.titleImageView.image = [UIImage imageNamed:@"find_radio_default@2x"];
    self.titleImageView.layer.cornerRadius = (self.bg.frame.size.width * 0.22) / 2;
    self.titleImageView.contentMode=UIViewContentModeScaleAspectFit;
    self.titleImageView.layer.masksToBounds = YES;
    [self.bg addSubview:self.titleImageView];

    
//    self.mineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-89) style:UITableViewStylePlain];
//    self.mineTableView.contentInset = UIEdgeInsetsMake(0.6*SCREEN_WIDTH, 0, 0, 0);//232.5
//    [self.view addSubview:self.mineTableView];
//    self.mineTableView.delegate = self;
//    self.mineTableView.dataSource = self;
//    [self.mineTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"mineTableViewCell"];
//    self.mineTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [self setAutomaticallyAdjustsScrollViewInsets:NO];
//    
//    
//    //背景图片
//    self.backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -0.6*SCREEN_WIDTH, SCREEN_WIDTH + 10, 0.6*SCREEN_WIDTH)];
//    UIImage *image = [UIImage imageNamed:@"find_radio_default@2x"];
//    self.backgroundImageView.image = [image boxblurImageWithBlur:0.8];
//    self.backgroundImageView.backgroundColor = [UIColor clearColor];
//    self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
//    
//    //用户头像
//    self.titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0.4*SCREEN_WIDTH, 0.4*SCREEN_WIDTH)];
//    self.titleImageView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_WIDTH * 0.32);
//    self.titleImageView.layer.masksToBounds = YES;
//    self.titleImageView.layer.cornerRadius = 0.2*SCREEN_WIDTH;
//    self.titleImageView.image = [UIImage imageNamed:@"user"];
//    self.titleImageView.backgroundColor = [UIColor clearColor];
//    [self.backgroundImageView addSubview:self.titleImageView];
//
//    
//    [self.mineTableView addSubview:self.backgroundImageView];
    
    
}

#pragma mark - 协议方法 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

#pragma mark - 协议方法 cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mineTableViewCell"];
    switch (indexPath.row) {
        case 0:
            //            NSLog(@"登陆");
            cell.textLabel.text = @"登陆";
            break;
        case 1:
            //            NSLog(@"夜间模式");
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.text = @"夜间模式";
            //创建switch
            UISwitch *nightSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.8, cell.contentView.bounds.size.height * 0.15, SCREEN_WIDTH * 0.15, cell.contentView.bounds.size.height * 0.6)];
            [cell addSubview:nightSwitch];
            nightSwitch.backgroundColor = [UIColor clearColor];
//            nightSwitch.tintColor = [UIColor whiteColor];
//            nightSwitch.onTintColor = [UIColor ];
            //设置状态
            if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"]) {
                [nightSwitch setOn:YES];
            } else {
                [nightSwitch setOn:NO];
            }
            [nightSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
            
            break;
        }
        case 2:
            //            NSLog(@"清除缓存");
        {
            cell.textLabel.text = @"清除缓存";
            
            break;
        }
        case 3: {
            cell.textLabel.text = @"关于我们";
            
        }
            break;
        default:
            break;
    }
    
    return cell;
}

#pragma mark - 选择按钮的动作方法
- (void)switchAction:(UISwitch *)switchBtn{
    if (switchBtn.isOn == YES) {
        //如果是夜间模式
        [[NSUserDefaults standardUserDefaults] setObject:@"night" forKey:@"Mode"];
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:@"day" forKey:@"Mode"];
    }
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:nil];
    
    
}

#pragma mark - 控制下拉放大
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //刚开始y的偏移量初始值就是-264
    //    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y;//加上导航栏高度,第一次是-200
    //    NSLog(@"y2 === %f",y);
    //
    if (y < -0.6*SCREEN_WIDTH) {
        CGRect frame = self.backgroundImageView.frame;
        frame.origin.y = y;//偏移了多少,
        frame.size.height =  -y;//tablview向下偏移了多少,高度就是多少
        self.backgroundImageView.frame = frame;
    }
}

#pragma mark - 协议方法 选中执行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    //清除缓存
    if (indexPath.row == 2) {
        //获取缓存数据
        NSUInteger size = [[SDImageCache sharedImageCache] getSize];
        //提示
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"缓存为%.02lfM\n 您确定删除吗?", size / 1024.0 / 1024.0] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *acc = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull            action) {
            //清除图片
            if (size != 0) {
                [[SDImageCache sharedImageCache] clearDisk];
            }
            
        }];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [alert addAction:acc];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:^{
        }];
    }
    
    //关于我们
    if (indexPath.row == 3) {
        
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
